<template>
	<view class="content">
		<z-paging ref="pagingRef" v-model="dataList" @query="queryList" :fixed="false" :empty-config="{tip: '暂无数据'}">

			<!-- 用户-->
			<view class="item" v-for="(item, index) in dataList" :key="index" @click="itemClick(item)">
				<!-- 人员信息结构保持不变 -->
				<view class="uni-flex uni-align-center top">
					<view class="photo-box">
						<image :src="`${$ImgBaseUrl}banner.png`" class="photo" />
						<text class="online">{{item.isOnLine===true?'在线':'离线'}}</text>
					</view>
					<view class="uni-flex-column" style="flex: 1">
						<view class="uni-flex uni-align-center uni-justify-between box1">
							<view class="uni-flex">
								<text class="title">{{item.name}}</text>
								<view class="real-name">
									<image :src="`${$ImgBaseUrl}findpeople_realname.png`" mode="widthFix" />
									<text class="text1">{{item.isRealName===true?'已实名':'未实名'}}</text>
								</view>
								<view class="sign-contract">
									<image :src="`${$ImgBaseUrl}findpeople_sign.png`" mode="widthFix" />
									<text class="text1">{{item.isSignService===true?'已签约':'未签约'}}</text>
								</view>
							</view>
							<view class="distance-box">
								<image :src="`${$ImgBaseUrl}detail_location.png`" mode="widthFix" class="location" />
								<text class="text1">{{item.distance}}km</text>
							</view>
						</view>
						<view class="box2">
							<text class="text1">{{item.sex==="0"?'男':item.sex==="1"?'女':'未知'}} | {{item.age}}岁 |
								服务</text>
							<text class="text2">{{item.closeCount}}</text>
							<text class="text1">单 | 评价</text>
							<text class="text2">{{item.comEvaluation}}</text>
							<text class="text1">分</text>
						</view>
					</view>
				</view>
				<view class="uni-flex box3">
					<text class="text1">可接业务: </text>
					<view class="con">
						<template v-if="item.services && item.services.length > 0">
							<view v-for="(s, i) in item.services" :key="i" class="text2">{{ s }}</view>
						</template>
						<template v-else>
							<view class="empty-text">暂无</view>
						</template>
					</view>
				</view>
				<view class="uni-flex box3">
					<text class="text1">擅长服务: </text>
					<view class="con">
						<template v-if="item.goodServices && item.goodServices.length > 0">
							<view v-for="(g, i) in item.goodServices" :key="i" class="project">{{ g }}</view>
						</template>
						<template v-else>
							<view class="empty-text">暂无</view>
						</template>
					</view>
				</view>
				<view class="footer">
					<view class="btn">试一下</view>
				</view>
			</view>

			<template #empty>
				<view>暂无数据</view>
			</template>
		</z-paging>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from "vue";
	import {
		browseReceiveList
	} from '@/api/browsinghistory.js'
	import {
		onShow
	} from '@dcloudio/uni-app';
	import {
		getLatitude,
		getLongitude
	} from "@/common/util.js";

	const props = defineProps({
		currentId: Number
	});

	const pagingRef = ref(null);
	const dataList = ref([]);

	// 监听currentId变化或使用onShow
	watch(() => props.currentId, (newVal) => {
		if (newVal === 3) { // 如果是服务页签
			pagingRef.value?.reload();
		}
	});

	// 或者使用onShow（需要确保组件在tab切换时会被销毁重建）
	onShow(() => {
		if (props.currentId === 3) {
			pagingRef.value?.reload();
		}
	});

	const queryList = async (pageNum, pageSize) => {
		try {
			const params = {
				browseType: 3, // 修改为服务对应的类型
				longitude: getLongitude(),
				latitude: getLatitude(),
				pageNum,
				pageSize
			};

			const res = await browseReceiveList(params);

			if (res && res.code === 200) {
				const newData = res.rows || [];
				if (pageNum === 1) {
					dataList.value = newData;
				} else {
					dataList.value = [...dataList.value, ...newData];
				}
				pagingRef.value.complete(newData, res.total <= pageNum * pageSize);
			} else {
				pagingRef.value.complete([], true);
			}
		} catch (error) {
			pagingRef.value.complete(false);
		}
	};
	// 点击项目
	const itemClick = (item) => {
		uni.navigateTo({
			url: "/pages/tabBars/findPeople/peopleDetail?jobId=$?id=" + item.userId
		});
	};


</script>

<style lang="scss" scoped>
	/* 保持原有样式不变 */
	.content {
		// padding: 20rpx;
		height: calc(100vh - 200rpx);
	}

	.empty-view {
		text-align: center;
		padding: 40rpx;
		color: #999;
		font-size: 28rpx;
	}

	.item {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;
		margin: 26rpx 24rpx 0;

		.top {
			padding-bottom: 18rpx;
			border-bottom: 1rpx solid #ededed;

			.photo-box {
				position: relative;

				.photo {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-right: 24rpx;
				}

				.online {
					position: absolute;
					bottom: 0;
					left: 10rpx;
					color: white;
					background: #6bc98d;
					border-radius: 17rpx;
					padding: 5rpx 0;
					font-weight: 500;
					width: 80rpx;
					text-align: center;
					font-size: 22rpx;
				}
			}

			.box1 {
				margin-bottom: 9rpx;

				.title {
					font-size: 32rpx;
					font-weight: 500;
					color: #333333;
					margin-right: 8rpx;
				}

				.real-name {
					background: #ffeae6;
					border-radius: 17px;
					border: 1px solid #ff6a47;
					// padding: 4rpx 18rpx;
					// margin-left: 8rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					width: 116rpx;
					height: 34rpx;

					image {
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}

					.text1 {
						font-weight: 500;
						font-size: 22rpx;
						color: #ff6a47;
					}
				}

				.sign-contract {
					background: #e5f0ff;
					border-radius: 17px;
					border: 1px solid #2f80ed;
					// padding: 4rpx 18rpx;
					margin-left: 8rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 116rpx;
					height: 34rpx;

					image {
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}

					.text1 {
						font-weight: 500;
						font-size: 22rpx;
						color: #2f80ed;
					}
				}

				.distance-box {
					display: flex;
					align-items: center;

					.location {
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}

					.text1 {
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
					}
				}
			}

			.box2 {
				.text1 {
					font-size: 24rpx;
					color: #999999;
				}

				.text2 {
					font-size: 24rpx;
					color: #2f80ed;
				}
			}
		}

		.box3 {
			align-items: flex-start;
			/* 顶部对齐 */
			width: 100%;
			margin-top: 16rpx;

			.text1 {
				font-size: 28rpx;
				color: #666666;
				flex-shrink: 0;
				/* 防止标签被压缩 */
			}

			.con {
				flex: 1;
				display: flex;
				flex-wrap: wrap;
				/* 关键属性：允许换行 */
				gap: 10rpx;
				/* 项目间距 */
				margin-left: 20rpx;

				.empty-text {
					font-size: 28rpx;
					color: #666666;
					flex-shrink: 0;
					/* 防止标签被压缩 */
				}

				.text2 {
					font-size: 28rpx;
					color: #333333;
				}

				.project {
					color: #2f80ed;
					font-size: 24rpx;
					font-weight: 500;
					background: #e5f0ff;
					padding: 6rpx 10rpx;
					margin-right: 14rpx;
					border-radius: 4rpx;
					white-space: nowrap;
					/* 单行显示 */
				}
			}
		}

		.footer {
			display: flex;
			justify-content: flex-end;
			/* 将子元素推到右侧 */
			width: 100%;
			margin-top: 20rpx;

			.btn {
				font-weight: 500;
				font-size: 30rpx;
				color: #ffffff;
				width: 163rpx;
				height: 68rpx;
				line-height: 68rpx;
				background: #2f80ed;
				border-radius: 34rpx;
				text-align: center;
			}
		}
	}
</style>